const app = getApp();
const util = require("../../../utils/util.js");

var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置

Page({
  data: {
    tabs: ["全部订单", "待付款", "待发货", "待收货", "退换货"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    allFormList: [],
    waitPayList: [],
    waitSendOrdersList: [],
    waitReceiveOrdersList: [],
    returnOrdersList: [],
    windowWidth: 0
  },


  tabClick: function(e) {
    let that = this
    console.log(e)
    let id = e.currentTarget.id
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    console.log(e.currentTarget.offsetLeft)
    // 点击全部订单
    if (id == 0) {
      wx.showLoading()
      // 请求全部订单列表
      let allOrderUrl = app.globalData.baseUrl + "weixin/allOrders"
      let cid = wx.getStorageSync('cid')
      let data = {
        cid: cid
      }
      util.requestLoading(allOrderUrl, data, "GET").then(res => {
        // console.log(res)
        that.setData({
          allFormList: res.data.ordersList
        })
        wx.hideLoading()
      })
    }
    // 点击待付款
    if (id == 1) {
      wx.showLoading()
      // 请求待付款订单列表
      let waitPayOrdersUrl = app.globalData.baseUrl + "weixin/waitPayOrders"
      let cid = wx.getStorageSync('cid')
      let data = {
        cid: cid
      }
      util.requestLoading(waitPayOrdersUrl, data, "GET").then(res => {
        // console.log(res)
        that.setData({
          waitPayList: res.data.ordersList
        })
        wx.hideLoading()
      })
    }
    // 点击待发货
    if (id == 2) {
      wx.showLoading()
      // 请求待发货订单列表
      let waitSendOrdersUrl = app.globalData.baseUrl + "weixin/waitSendOrders"
      let cid = wx.getStorageSync('cid')
      let data = {
        cid: cid
      }
      util.requestLoading(waitSendOrdersUrl, data, "GET").then(res => {
        // console.log(res)
        that.setData({
          waitSendOrdersList: res.data.ordersList
        })
        wx.hideLoading()
      })
    }
    // 点击待收货
    if (id == 3) {
      wx.showLoading()
      // 请求待收货订单列表
      let waitReceiveOrdersUrl = app.globalData.baseUrl + "weixin/waitReceiveOrders"
      let cid = wx.getStorageSync('cid')
      let data = {
        cid: cid
      }
      util.requestLoading(waitReceiveOrdersUrl, data, "GET").then(res => {
        // console.log(res)
        that.setData({
          waitReceiveOrdersList: res.data.ordersList
        })
        wx.hideLoading()
      })
    }
    // 点击退换货
    if (id == 4) {
      wx.showLoading()
      // 请求退换货订单列表
      let returnOrdersListUrl = app.globalData.baseUrl + "weixin/returnOrdersList"
      let cid = wx.getStorageSync('cid')
      let data = {
        cid: cid
      }
      util.requestLoading(returnOrdersListUrl, data, "GET").then(res => {
        // console.log(res)
        that.setData({
          returnOrdersList: res.data.ordersList
        })
        wx.hideLoading()
      })
    }
  },

  // 删除订单
  deleteOrder: function(e) {
    let that = this
    // console.log(e)
    wx.showModal({
      title: '确认删除',
      content: '您确认删除该订单吗',
      confirmText: "确认",
      cancelText: "取消",
      success: function(res) {
        // console.log(res);
        if (res.confirm) {
          // 发送删除订单请求
          let deleteOrderUrl = app.globalData.baseUrl + "weixin/deleteOrder"
          let data = {
            orderId: e.currentTarget.id
          }
          util.requestLoading(deleteOrderUrl, data, "GET").then(res => {
            // console.log(res)
            // 请求全部订单列表
            let allOrderUrl = app.globalData.baseUrl + "weixin/allOrders"
            let cid = wx.getStorageSync('cid')
            let data = {
              cid: cid
            }
            util.requestLoading(allOrderUrl, data, "GET").then(res => {
              // console.log(res)
              that.setData({
                allFormList: res.data.ordersList
              })
            })
          })
        }
      }
    })
  },

  // 再次购买
  buyAgain: function(e) {
    let that = this
    // console.log(e.currentTarget.id) // orderId
    // 发送再次购买请求
    let buyAgainUrl = app.globalData.baseUrl + "weixin/buyAgain"
    let data = {
      orderId: e.currentTarget.id
    }
    util.requestLoading(buyAgainUrl, data, "GET").then(res => {
      // console.log(res)
      if (res.data.status) {
        wx.reLaunch({
          url: '../../shopCar/shopCar',
        })
      }
    })
  },

  // 取消订单
  cancelOrder: function(e) {
    let that = this
    // console.log(e.currentTarget.id)
    wx.showModal({
      title: '取消订单',
      content: '您确认取消该订单吗',
      confirmText: "确认",
      cancelText: "取消",
      success: function(res) {
        // console.log(res);
        if (res.confirm) {
          // 发送取消订单请求
          let cancleOrderUrl = app.globalData.baseUrl + "weixin/cancleOrder"
          let data = {
            orderId: e.currentTarget.id
          }
          util.requestLoading(cancleOrderUrl, data, "GET").then(res => {
            // console.log(res)
            // 请求待付款订单列表
            let waitPayOrdersUrl = app.globalData.baseUrl + "weixin/waitPayOrders"
            let cid = wx.getStorageSync('cid')
            let data = {
              cid: cid
            }
            util.requestLoading(waitPayOrdersUrl, data, "GET").then(res => {
              // console.log(res)
              that.setData({
                waitPayList: res.data.ordersList
              })
            })
          })
        }
      }
    })
  },

  // 付款
  toPayDetails: function(e) {
    let that = this
    let waitPayList = that.data.waitPayList
    let orderId = e.currentTarget.id
    let productName, money
    // console.log(waitPayList)
    for (let i = 0; i < waitPayList.length; i++) {
      if (waitPayList[i].orderId == orderId) {
        productName = waitPayList[i].order[0].productName
        money = waitPayList[i].shouldPay
      }
    }
    // console.log(money)
    wx.showLoading()
    // 发送支付请求
    let payUrl = app.globalData.baseUrl + "weixin/pay"
    let openid = wx.getStorageSync('openId')
    let data = {
      openid: openid,
      // productName: productName,
      orderId: orderId,
      money: money
    }
    util.requestLoading(payUrl, data, "GET").then(res => {
      console.log(res)
      if (res.statusCode == 200) {
        wx.hideLoading()
        let openId = wx.getStorageSync("openId")
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: 'MD5',
          paySign: res.data.paySign,
          success(res) {
            console.log(res)
            // // 发送成功回调请求
            // let payResultUrl = app.globalData.baseUrl + "weixin/payResult"
            // let data = {
            //   orderId: orderId,
            //   money: money, 
            //   status: 0
            // } 
            // util.requestLoading(payResultUrl, data, "GET").then(res => {
            //   // console.log(res)
            // })
            wx.showModal({
              content: '支付成功',
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  wx.reLaunch({
                    url: '../../index/index'
                  })
                }
              }
            })
          },
          fail(res) {
            console.log(res)
            // // 发送失败回调请求
            // let payResultUrl = app.globalData.baseUrl + "weixin/payResult"
            // let data = {
            //   orderId: orderId,
            //   money: money,
            //   status: 1
            // }
            // util.requestLoading(payResultUrl, data, "GET").then(res => {
            //   // console.log(res)
            // })
          }
        })
      }
    })
  },

  // 待支付详情
  orderDetail: function(e) {
    // console.log(e)
    let that = this
    let orderId = e.currentTarget.id
    let index = e.currentTarget.dataset.index
    switch (index) {
      case 1:
        wx.navigateTo({
          url: 'payDetails/payDetails?orderId=' + orderId
        })
        break
      case 2:
        wx.navigateTo({
          url: 'deliverGoods/deliverGoods?orderId=' + orderId
        })
        break
      case 3:
        wx.navigateTo({
          url: 'waitforReceiving/waitforReceiving?orderId=' + orderId
        })
        break
      case 4:
        wx.navigateTo({
          url: 'payDetails/payDetails?orderId=' + orderId
        })
        break
      case 5:
        wx.navigateTo({
          url: 'payDetails/payDetails?orderId=' + orderId
        })
        break
      case 6:
        wx.navigateTo({
          url: 'returnDetails/returnDetails?orderId=' + orderId
        })
        break
      case 7:
        wx.navigateTo({
          url: 'returnDetails/returnDetails?orderId=' + orderId
        })
        break
      case 8:
        wx.navigateTo({
          url: 'returnDetails/returnDetails?orderId=' + orderId
        })
        break
      case 9:
        wx.navigateTo({
          url: 'returnDetails/returnDetails?orderId=' + orderId
        })
        break
      case 10:
        wx.navigateTo({
          url: 'returnDetails/returnDetails?orderId=' + orderId
        })
        break
      case 11:
        wx.navigateTo({
          url: 'payDetails/payDetails?orderId=' + orderId
        })
        break
    }
  },

  // 催发货
  pushDelivery: function(e) {
    // 请求待收货订单列表
    let promptSendUrl = app.globalData.baseUrl + "weixin/promptSend"
    let data = {
      orderId: e.currentTarget.id
    }
    util.requestLoading(promptSendUrl, data, "GET").then(res => {
      // console.log(res)
      wx.showToast({
        title: '卖家已收到',
      })
    })
  },

  // 查看物流
  checkLogistics: function(e) {
    // console.log(e.currentTarget.id)
    let orderId = e.currentTarget.id
    wx.navigateTo({
      url: 'logisticsDetails/logisticsDetails?orderId=' + orderId
    })
  },

  // 确认收货
  confirmReceipt: function(e) {
    // console.log(e)
    let that = this
    // console.log(e.currentTarget.id)
    let orderId = e.currentTarget.id
    wx.showModal({
      title: '确认收货',
      content: '您确认已收到商品吗',
      confirmText: "确认",
      cancelText: "取消",
      success: function(res) {
        // console.log(res);
        if (res.confirm) {
          // 发送确认收货请求
          let sureReceiveUrl = app.globalData.baseUrl + "weixin/sureReceive"
          let data = {
            orderId: orderId
          }
          util.requestLoading(sureReceiveUrl, data, "GET").then(res => {
            // console.log(res)
            // 请求待收货订单列表
            let waitReceiveOrdersUrl = app.globalData.baseUrl + "weixin/waitReceiveOrders"
            let cid = wx.getStorageSync('cid')
            let data = {
              cid: cid
            }
            util.requestLoading(waitReceiveOrdersUrl, data, "GET").then(res => {
              // console.log(res)
              that.setData({
                waitReceiveOrdersList: res.data.ordersList
              })
            })
          })
        }
      }
    })
  },

  onLoad: function(options) {
    var that = this
    wx.showLoading()
    // 请求全部订单列表
    let allOrderUrl = app.globalData.baseUrl + "weixin/allOrders"
    let cid = wx.getStorageSync('cid')
    let data = {
      cid: cid
    }
    util.requestLoading(allOrderUrl, data, "GET").then(res => {
      // console.log(res)
      that.setData({
        allFormList: res.data.ordersList
      })
      wx.hideLoading()
    })

    // 请求待付款订单列表
    let waitPayOrdersUrl = app.globalData.baseUrl + "weixin/waitPayOrders"
    let data2 = {
      cid: cid
    }
    util.requestLoading(waitPayOrdersUrl, data2, "GET").then(res => {
      // console.log(res)
      that.setData({
        waitPayList: res.data.ordersList
      })
      wx.hideLoading()
    })

    // 请求待发货订单列表
    let waitSendOrdersUrl = app.globalData.baseUrl + "weixin/waitSendOrders"
    let data3 = {
      cid: cid
    }
    util.requestLoading(waitSendOrdersUrl, data3, "GET").then(res => {
      // console.log(res)
      that.setData({
        waitSendOrdersList: res.data.ordersList
      })
      wx.hideLoading()
    })

    // 请求待收货订单列表
    let waitReceiveOrdersUrl = app.globalData.baseUrl + "weixin/waitReceiveOrders"
    let data4 = {
      cid: cid
    }
    util.requestLoading(waitReceiveOrdersUrl, data4, "GET").then(res => {
      // console.log(res)
      that.setData({
        waitReceiveOrdersList: res.data.ordersList
      })
      wx.hideLoading()
    })

    // 请求退换货订单列表
    let returnOrdersListUrl = app.globalData.baseUrl + "weixin/returnOrdersList"
    let data5 = {
      cid: cid
    }
    util.requestLoading(returnOrdersListUrl, data5, "GET").then(res => {
      // console.log(res)
      that.setData({
        returnOrdersList: res.data.ordersList
      })
      wx.hideLoading()
    })

    that.setData({
      activeIndex: options.activeIndex,
      sliderOffset: options.sliderOffset
    })
  },

  onShow:function(e) {
    
  }

});